"""
@author: 江同学呀
@file: binary_field.py
@date: 2024/7/25 14:09
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/binary.html

    Binary value encoded as a Base64 string.
    编码为 Base64 字符串的二进制值。
"""
from abc import ABC
from typing import Type, Union, Dict

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import DocValuesAttribute, StoreAttribute
from espc.utils.build_add_attr import build_add_attr


class BinaryField(_BaseField, DocValuesAttribute, StoreAttribute, ABC):
    """
    二进制 字段
    """
    _field_type: str = "binary"

    def __init__(
            self, field_name: str = None, value: bytes = None, default: bytes = None, doc_values: bool = None,
            store: bool = None
    ):
        super().__init__(field_name=field_name, value=value, default=default, doc_values=doc_values, store=store)
        return

    def __get__(self, instance: _Base, owner: Type["BinaryField"]) -> Union["BinaryField", bytes]:
        return super(BinaryField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: bytes):
        super(BinaryField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> bytes:
        return self._value.encode() if isinstance(self._value, str) else self._value

    @value.setter
    def value(self, value: bytes):
        self._value: bytes = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type,
        }
        return mapping




